#include <interface\winbar.h>
#include <interface\window.h>
#include <interface\frame.h>
#include <interface\toggleButton.h>
#include <interface\gl.h>

#include <cstdio>

winBar::winBar():   fieldSet(),
                    close( NULL ),
                    minimize( NULL ),
                    lock( NULL ),
                    caption( NULL ),
                    dragEnabled( true ),
                    margin( DFT_WBAR_MARGIN ){
    ELname = "<winBar>";
    setHeight(DFT_WBAR_HGT);
}
winBar::winBar(window* win):    fieldSet( ),
                                close( NULL ),
                                minimize( NULL ),
                                lock( NULL ),
                                caption( NULL ),
                                dragEnabled( true ),
                                margin( DFT_WBAR_MARGIN ){
    ELname = "<winBar>";
    setHeight(DFT_WBAR_HGT);
    parentWin = win;
}
winBar::winBar(unsigned int mode):  fieldSet(),
                                    close( NULL ),
                                    minimize( NULL ),
                                    lock( NULL ),
                                    caption( NULL ),
                                    dragEnabled( true ),
                                    margin( DFT_WBAR_MARGIN ){
    setMode( mode );
    ELname = "<winBar>";
    setHeight(DFT_WBAR_HGT);
}
winBar::winBar(window* win, unsigned int mode): fieldSet( ),
                                                close( NULL ),
                                                minimize( NULL ),
                                                lock( NULL ),
                                                dragEnabled( true ),
                                                margin( DFT_WBAR_MARGIN ){
    setMode( mode );
    ELname = "<winBar>";
    setHeight(DFT_WBAR_HGT);
    parentWin = win;
}
winBar::~winBar(){
    if( close ) delete close;
    if( minimize ) delete minimize;
    if( lock ) delete lock;
    if( caption ) delete caption;
}

void winBar::setParentSet(fieldSet* set){
    parentSet = set;
    if( set ){
        setWidth(set->getWidth());
        move(0,set->getHeight());
        dragEnabled = !((frame*)set)->getLocked();
    }
}

void winBar::setDrag(bool val){
    dragEnabled = val;
}
bool winBar::getDrag(){
    return dragEnabled;
}

void winBar::setFont(font* f){
    ft = f;
    if( caption ) caption->setFont(ft);
}
void winBar::setCaption(string str){
    if( caption ) caption->setCaption(str);
}
string winBar::getCaption(){
    if( caption ) return caption->getCaption();
    return "";
}

void winBar::setMode(unsigned int mode){
    if( !parentSet ) return;
    unsigned short size = getHeight() - 2* margin;
    unsigned short rightOffset = 0;
    if( mode & WBAR_CAPTION ){
        if( !caption ){
            caption = new textDisplay();
            caption->setFont(ft);
        }
    }
    else{
        if( caption ){
            delete caption;
        }
    }
    if( mode & WBAR_CLOSE_BTN ){
        if( !close ){
            rightOffset += size + margin;
            close = new barCloseButton(parentWin);
            close->setRect(getWidth()-rightOffset,margin,size,size);
            close->setParentSet(this);
            close->setStyle(styleS);
        }
    }
    else{
        if( close ){
            parentWin->remFromElmts(close);
            delete close;
        }
    }
    if( mode & WBAR_MINI_BTN ){
        if( !minimize ){
            rightOffset += size + margin;
            minimize = new barMinimizeButton(parentWin);
            minimize->setRect(getWidth()-rightOffset,margin,size,size);
            minimize->setParentSet(this);
            minimize->setStyle(styleS);
            minimize->setStatus(((frame*)parentSet)->getMinimized());
        }
    }
    else{
        if( minimize ){
            parentWin->remFromElmts(minimize);
            delete minimize;
        }
    }
    if( mode & WBAR_LOCK_BTN ){
        if( !lock ){
            rightOffset += size + margin;
            lock = new barLockButton(parentWin);
            lock->setRect(getWidth()-rightOffset,margin,size,size);
            lock->setParentSet(this);
            lock->setStyle(styleS);
            lock->setStatus(((frame*)parentSet)->getLocked());
        }
    }
    else{
        if( lock ){
            parentWin->remFromElmts(lock);
            delete lock;
        }
    }
}

void winBar::display(){
    if( !styleS ) return;
    winElmt::display();
    glDisable(GL_SCISSOR_TEST);
    glColor(bkgColFromSS(elm_winBar));
    int Xoff = 0;
    int Yoff = 0;
    getOffset(&Xoff,&Yoff);
    glDrawRect(enc,Xoff,Yoff);
    if( caption ){
        glColor(txtColFromSS(elm_winBar));
        caption->display(Xoff+enc.left+margin,Yoff+enc.bottom+margin);
    }
    elDraw();
}

void winBar::draggingEvent(int x,int y){
    winElmt::draggingEvent(x,y);
    if( !dragEnabled ) return;
    if( parentSet ) parentSet->move(x,y);
    else move(x,y);
}

void winBar::activeGetEvent(){
    winElmt::activeGetEvent();
    if( parentSet ){
        if( !parentSet->isActive() ) parentSet->activeGetEvent();
    }
}

barCloseButton::barCloseButton():   button(){
    ELname = "<barCloseBTN>";
}
barCloseButton::barCloseButton(window* win):    button(win){
    ELname = "<barCloseBTN>";
}
barCloseButton::~barCloseButton(){}
void barCloseButton::mouseClickEvent(int){
    if( parentWin && parentSet ) parentWin->eventListener( new frameCloseEvent(parentSet->getParentSet()) );
}

barMinimizeButton::barMinimizeButton():   toggleButton(){
    ELname = "<barMinimizeBTN>";
}
barMinimizeButton::barMinimizeButton(window* win):    toggleButton(win){
    ELname = "<barMinimizeBTN>";
}
barMinimizeButton::~barMinimizeButton(){}
void barMinimizeButton::mouseClickEvent(int){
    status = !status;
    if( parentWin && parentSet ) parentWin->eventListener( new frameMinimizeEvent(parentSet->getParentSet(),status) );
}

barLockButton::barLockButton():   toggleButton(){
    ELname = "<barLockBTN>";
}
barLockButton::barLockButton(window* win):    toggleButton(win){
    ELname = "<barLockBTN>";
}
barLockButton::~barLockButton(){}
void barLockButton::mouseClickEvent(int){
    status = !status;
    if( parentWin && parentSet ) parentWin->eventListener( new frameLockEvent(parentSet->getParentSet(),status) );
}

frameCloseEvent::frameCloseEvent(winElmt* elm): event(elm){
    type = event_type_frame_close;
}
frameMinimizeEvent::frameMinimizeEvent(winElmt* elm, bool val): Bevent(elm,val){
    type = event_type_frame_minimize;
}
frameLockEvent::frameLockEvent(winElmt* elm,bool val): Bevent(elm,val){
    type = event_type_frame_lock;
}
